package com.yllt4cloud.ddauth.service.impl;

import com.yllt4cloud.common.ddbase.domain.Clerk;
import com.yllt4cloud.common.ddbase.domain.User;
import com.yllt4cloud.common.ddbase.util.JsonUtil;
import com.yllt4cloud.ddauth.mapper.ClerkMapper;
import com.yllt4cloud.ddauth.mapper.UserMapper;
import com.yllt4cloud.ddauth.service.ILoginService;
import org.apache.commons.collections.CollectionUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @description:
 * @author: yaoxj
 * @create: 2019-12-09 14:55
 **/
@Service
public class LoginServiceImpl  implements ILoginService {
    private static final Logger log = LoggerFactory.getLogger(LoginServiceImpl.class);

    @Autowired
    private UserMapper userMapper;

    @Autowired
    private ClerkMapper clerkMapper;

    @Override
    public User login(User user) {
        Map<String, Object> paramMap = JsonUtil.parseMap(user);
        List<User> resultList = userMapper.query(paramMap);
        if (CollectionUtils.isNotEmpty(resultList)) {
            return resultList.get(0);
        }
        return null;
    }

    /**
     * 验证管理员的账号是否存在
     *
     * @param loginName
     * @return
     */
    public Boolean validateName(String loginName) {
        Map<String, Object> paramMap = new HashMap<String, Object>();
        paramMap.put("loginName", loginName);
        List<User> resultList = userMapper.query(paramMap);
        if (resultList == null || resultList.size() == 0) {
            return false;
        } else {
            return true;
        }

    }

    @Override
    public Clerk loginClerk(Clerk clerk) {
        Map<String, Object> paramMap = JsonUtil.parseMap(clerk);
        List<Clerk> resultList = clerkMapper.query(paramMap);
        if (CollectionUtils.isNotEmpty(resultList)) {
            return resultList.get(0);
        }
        return null;
    }
}
